package logic

import (
	"context"
	"shop/model/mysql"

	"shop/shoprpc/internal/svc"
	"shop/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *shoprpc.ProductAddRequest) (*shoprpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	data, _ := product.FindProduct(mysql.Db, in.Title)
	if data.ID == 0 {
		m := mysql.Product{
			Image:  in.Image,
			Title:  in.Title,
			Price:  float64(in.Price),
			Stock:  in.Stock,
			Summit: in.Summit,
		}
		mysql.Db.Debug().Create(&m)
	}

	return &shoprpc.ProductAddResponse{
		ProductId: int64(product.ID),
	}, nil
}
